<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 充值订单管理
 *
 * @icon fa fa-circle-o
 */
class RechargeOrder extends Backend
{

    /**
     * RechargeOrder模型对象
     * @var \app\admin\model\RechargeOrder
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\RechargeOrder;
        $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['users'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','order_sn','order_status','money','add_time','zmoney','bonus','bonus_desc']);
                $row->visible(['users']);
				$row->getRelation('users')->visible(['nickname','phone']);
            }

            foreach($list as &$v){
                if(!empty($v['bonus'])){
                    $bonus_desc = '';
                    $bonus = json_decode($v['bonus'],true);
                    foreach($bonus as $v2){
                        $bonus_name = db("bonus")->where(['id'=>$v2['bonus_id']])->value('name');
                        $bonus_desc .= $bonus_name."x".$v2['num'].',';
                    }
                    $v['bonus_desc'] = trim($bonus_desc,',');
                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

}
